package com.zhoug.zmuidemo.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.os.Bundle;
import android.widget.ImageView;

import com.zhoug.common.utils.CanvasUtils;
import com.zhoug.zmuidemo.R;

public class CanvasActivity extends AppCompatActivity {

    private ImageView mImageViewSrc;
    private ImageView mImageViewDst;
    private ImageView mImageViewSrcOver;
    private ImageView mImageViewDstOver;
    private ImageView mImageViewSrcIn;
    private ImageView mImageViewdstIn;
    private ImageView mImageViewsrcOut;
    private ImageView mImageViewdstOut;
    private ImageView mImageViewsrcATop;
    private ImageView mImageViewdstATop;
    private ImageView mImageViewxor;
    private ImageView mImageViewdarken;
    private ImageView mImageViewlighten;
    private ImageView mImageViewmultiply;
    private ImageView mImageViewscreen;
    private ImageView mSRCIN;
    private ImageView mDSTIN;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canvas);
        findViews();


        init();
    }

    private int width = 1000;
    private int height = 1000;
    private int BLUE = Color.parseColor("#5588ff");
    private int GREEN = Color.parseColor("#ffff00");
    Rect src = new Rect(400, 400, 900, 900);
    int cx = 350;
    int cy = 350;
    int radius = 350;

    private Paint getPaint() {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);

        return paint;
    }

    private void init() {


        drawSrc();
        drawDst();
        drawSrcOver();
        drawDstOver();
        drawSrcIn();
        SRC_OUT();
        SRC_ATOP();
        DST_ATOP();
        DST_OUT();
        LIGHTEN();
        SCREEN();
        DARKEN();
        drawDstIn();
        XOR();
        MULTIPLY();

        //
        _SRC_IN();
        _DST_IN();
    }

    private void drawSrc() {
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
//        canvas.drawColor(Color.WHITE);
        Paint paint = getPaint();
        paint.setColor(BLUE);
        canvas.drawRect(src, paint);
        mImageViewSrc.setImageBitmap(bitmap);
    }

    private void drawDst() {
        Paint paint = getPaint();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
//        canvas.drawColor(Color.WHITE);

        paint.setColor(GREEN);
        canvas.drawCircle(cx, cy, radius, paint);
        mImageViewDst.setImageBitmap(bitmap);
    }

    private void drawSrcOver() {
        Paint paint = getPaint();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
//        canvas.drawColor(Color.WHITE);
        paint.setColor(GREEN);
        canvas.drawCircle(cx, cy, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
        paint.setColor(BLUE);
        canvas.drawRect(src, paint);

        mImageViewSrcOver.setImageBitmap(bitmap);
    }

    private void drawDstOver() {
        Paint paint = getPaint();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
//        canvas.drawColor(Color.WHITE);
        paint.setColor(GREEN);
        canvas.drawCircle(cx, cy, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
        paint.setColor(BLUE);
        canvas.drawRect(src, paint);

        mImageViewDstOver.setImageBitmap(bitmap);
    }

    private void drawSrcIn() {
        Paint paint = getPaint();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        paint.setColor(GREEN);
        canvas.drawCircle(cx, cy, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        paint.setColor(BLUE);
        canvas.drawRect(src, paint);


        mImageViewSrcIn.setImageBitmap(bitmap);
    }


    private Bitmap getSrc(){
        Paint paint = getPaint();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        paint.setColor(BLUE);
        canvas.drawRect(src, paint);

        return bitmap;
    }


    private void _SRC_IN() {
        Paint paint = getPaint();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        paint.setColor(GREEN);
        canvas.drawCircle(cx, cy, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        paint.setColor(BLUE);
        Bitmap src= getSrc();
        canvas.drawBitmap(src,0,0,paint);
        mSRCIN.setImageBitmap(bitmap);
    }

    private void _DST_IN() {
        Paint paint = getPaint();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        paint.setColor(GREEN);
        canvas.drawCircle(cx, cy, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        paint.setColor(BLUE);
        Bitmap src= getSrc();
        canvas.drawBitmap(src,0,0,paint);
        mDSTIN.setImageBitmap(bitmap);
    }

    private void SRC_ATOP() {
        Paint paint = getPaint();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        paint.setColor(GREEN);
        canvas.drawCircle(cx, cy, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
        paint.setColor(BLUE);
        canvas.drawRect(src, paint);

        mImageViewsrcATop.setImageBitmap(bitmap);
    }


    private void drawDstIn() {
        Paint paint = getPaint();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        paint.setColor(GREEN);
        canvas.drawCircle(cx, cy, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        paint.setColor(BLUE);
        canvas.drawRect(src, paint);

        mImageViewdstIn.setImageBitmap(bitmap);
    }

    private void SRC_OUT() {
        Paint paint = getPaint();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        paint.setColor(GREEN);
        canvas.drawCircle(cx, cy, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
        paint.setColor(BLUE);
        canvas.drawRect(src, paint);

        mImageViewsrcOut.setImageBitmap(bitmap);
    }

    private void DST_OUT() {
        Paint paint = getPaint();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        paint.setColor(GREEN);
        canvas.drawCircle(cx, cy, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        paint.setColor(BLUE);
        canvas.drawRect(src, paint);

        mImageViewdstOut.setImageBitmap(bitmap);
    }


    private void DST_ATOP() {
        Paint paint = getPaint();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        paint.setColor(GREEN);
        canvas.drawCircle(cx, cy, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP));
        paint.setColor(BLUE);
        canvas.drawRect(src, paint);

        mImageViewdstATop.setImageBitmap(bitmap);
    }

    private void XOR() {
        Paint paint = getPaint();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        paint.setColor(GREEN);
        canvas.drawCircle(cx, cy, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
        paint.setColor(BLUE);
        canvas.drawRect(src, paint);

        mImageViewxor.setImageBitmap(bitmap);
    }

    private void DARKEN() {
        Paint paint = getPaint();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        paint.setColor(GREEN);
        canvas.drawCircle(cx, cy, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN));
        paint.setColor(BLUE);
        canvas.drawRect(src, paint);

        mImageViewdarken.setImageBitmap(bitmap);
    }

    private void LIGHTEN() {
        Paint paint = getPaint();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        paint.setColor(GREEN);
        canvas.drawCircle(cx, cy, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.LIGHTEN));
        paint.setColor(BLUE);
        canvas.drawRect(src, paint);

        mImageViewlighten.setImageBitmap(bitmap);
    }

    private void MULTIPLY() {
        Paint paint = getPaint();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        paint.setColor(GREEN);
        canvas.drawCircle(cx, cy, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
        paint.setColor(BLUE);
        canvas.drawRect(src, paint);

        mImageViewmultiply.setImageBitmap(bitmap);
    }

    private void SCREEN() {
        Paint paint = getPaint();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        paint.setColor(GREEN);
        canvas.drawCircle(cx, cy, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));
        paint.setColor(BLUE);
        canvas.drawRect(src, paint);

        mImageViewscreen.setImageBitmap(bitmap);
    }


    private void findViews() {
        mImageViewSrc = findViewById(R.id.imageViewSrc);
        mImageViewDst = findViewById(R.id.imageViewDst);
        mImageViewSrcOver = findViewById(R.id.imageViewSrcOver);
        mImageViewDstOver = findViewById(R.id.imageViewDstOver);
        mImageViewSrcIn = findViewById(R.id.imageViewSrcIn);
        mImageViewdstIn = findViewById(R.id.imageViewdstIn);
        mImageViewsrcOut = findViewById(R.id.imageViewsrcOut);
        mImageViewdstOut = findViewById(R.id.imageViewdstOut);
        mImageViewsrcATop = findViewById(R.id.imageViewsrcATop);
        mImageViewdstATop = findViewById(R.id.imageViewdstATop);
        mImageViewxor = findViewById(R.id.imageViewxor);
        mImageViewdarken = findViewById(R.id.imageViewdarken);
        mImageViewlighten = findViewById(R.id.imageViewlighten);
        mImageViewmultiply = findViewById(R.id.imageViewmultiply);
        mImageViewscreen = findViewById(R.id.imageViewscreen);
        mSRCIN = findViewById(R.id.SRC_IN);
        mDSTIN = findViewById(R.id.DST_IN);
    }
}